<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Example
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial075.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial070.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial077.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc147">10.6</A>&nbsp;&nbsp;Example</H2>

<A NAME="@default282"></A>
The following program computes so-called Steiner triplets.
The problem is to compute triplets of numbers between 1 and N,
such that any two triplets have at most one element in common.

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- lib(ic_sets).
:- lib(ic).

steiner(N, Sets) :-
        NB is N * (N-1) // 6,           % compute number of triplets
        intsets(Sets, NB, 1, N),        % initialise the set variables
        ( foreach(S,Sets) do
            #(S,3)                      % constrain their cardinality
        ),
        ( fromto(Sets,[S1|Ss],Ss,[]) do
            ( foreach(S2,Ss), param(S1) do
                #(S1 /<CODE>\</CODE> S2, C),         % constrain the cardinality
                C #=&lt; 1                 % of pairwise intersections
            )
        ),
        label_sets(Sets).               % search

label_sets([]).
label_sets([S|Ss]) :-
        insetdomain(S,_,_,_),
        label_sets(Ss).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
Running this program yields the following first solution:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- steiner(9,X).

X = [[1, 2, 3], [1, 4, 5], [1, 6, 7], [1, 8, 9],
     [2, 4, 6], [2, 5, 8], [2, 7, 9], [3, 4, 9],
     [3, 5, 7], [3, 6, 8], [4, 7, 8], [5, 6, 9]] More? (;)
</PRE></BLOCKQUOTE>
<HR>
<A HREF="tutorial075.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial070.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial077.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
